package com.app.micaihu.view.main.dialog

import android.annotation.SuppressLint
import android.content.DialogInterface
import android.content.Intent
import com.app.base.BaseDialogFragment
import com.app.micaihu.R
import com.app.micaihu.bean.config.AppControl.Mission
import com.app.micaihu.configure.ConfigKeyConstant
import com.app.micaihu.manager.UserInforManager
import com.app.micaihu.utils.ImageLoadUtils
import com.app.micaihu.view.user.usertask.UserTaskActivity
import com.app.micaihu.view.utils.DownloadAgainUtils
import com.app.utils.util.DateUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.StringUtils
import kotlinx.android.synthetic.main.mian_login_dialog.*

class MainLoginDialogFragment : BaseDialogFragment() {

    override fun getLayoutId() = R.layout.mian_login_dialog

    @SuppressLint("SetTextI18n")
    override fun setData() {
        ImageLoadUtils.loadImageNoScaleType(R.mipmap.main_login_huniu, ivHuniu)
        val userInfo = UserInforManager.getInstance().userInfor
        if (userInfo != null) {
            val task = userInfo.task
            if (task != null) {
                tvRankName.text = "报告" + task.rankName
            }
        }
        val missionStr = SPUtils.getInstance().getString(ConfigKeyConstant.mission)
        if (missionStr != null) {
            val mission = GsonUtils.fromJson(missionStr, Mission::class.java)
            if (mission != null) {
                tvTaskTitle.text = " " + mission.login_gold + "金币" + mission.login_score + "积分"
                tvTaskComment.text = "评论文章金币＋" + mission.comment_gold + " 军功＋" + mission.comment_currency
                tvTaskShare.text = "分享文章金币＋" + mission.share_gold + " 军功＋" + mission.share_currency
            }
        }
    }

    override fun addListeners() {
        tvOk?.setOnClickListener {
            dismiss()
            startActivity(Intent(context, UserTaskActivity::class.java))
        }
        ivClose?.setOnClickListener { dismiss() }
    }

    override fun onDismiss(dialog: DialogInterface) {
        SPUtils.getInstance().put(ConfigKeyConstant.isShowLoginDialogData, DateUtils.getCurrentTime(DateUtils.formatDefault))

        val currentTime = DateUtils.getCurrentTime(DateUtils.formatDefault)
        val isShowPopupDialogData = SPUtils.getInstance().getString(ConfigKeyConstant.isShowPopupDialogData)
        if (currentTime == isShowPopupDialogData) {
            DownloadAgainUtils.showDownLoadDialog(parentFragmentManager)
            return
        }
        val popupImg = SPUtils.getInstance().getString(ConfigKeyConstant.popupImg, "")
        val popupStatus = SPUtils.getInstance().getString(ConfigKeyConstant.popupStatus, "")
        if (!StringUtils.isEmpty(popupImg) && "1" == popupStatus) {
            val mainPopupDialogFragment = MainPopupDialogFragment()
            mainPopupDialogFragment.show(parentFragmentManager)
        } else {
            DownloadAgainUtils.showDownLoadDialog(parentFragmentManager)
        }
        super.onDismiss(dialog)
    }

    override fun canceledOnTouchOutside(): Boolean {
        return true
    }
}